package com.iknow.xmpp.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.provider.Settings.System;
import com.iknow.IKnow;
import com.iknow.util.Loger;
import com.iknow.xmpp.extention.IKnowUserProvider;
import com.iknow.xmpp.extention.PingProvider;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import com.iknow.xmpp.smack.avatar.AvatarMetadataProvider;
import com.iknow.xmpp.smack.avatar.AvatarProvider;
import com.mobclick.android.MobclickAgent;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.Roster.SubscriptionMode;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.ChatStateExtension.Provider;
import org.jivesoftware.smackx.provider.DelayInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverItemsProvider;
import org.jivesoftware.smackx.pubsub.provider.EventProvider;
import org.jivesoftware.smackx.pubsub.provider.ItemProvider;
import org.jivesoftware.smackx.pubsub.provider.ItemsProvider;
import org.jivesoftware.smackx.pubsub.provider.PubSubProvider;

public class IKnowXmppService extends Service
{
  private static final int DEFAULT_XMPP_PORT = 5222;
  public static final int NOTIFICATION_STATUS_ID = 100;
  private static final String TAG = "IKnowXmppService";
  private IXmppFacade.Stub mBind;
  private XmppConnectionAdapter mConnection;
  private ConnectionConfiguration mConnectionConfiguration;
  private Handler mHandler;
  private String mLogin;
  private NotificationManager mNotificationManager;
  private BeemServiceBroadcastReceiver mOnOffReceiver;
  private boolean mOnOffReceiverIsRegistered;
  private String mPassword;
  private BeemServicePreferenceListener mPreferenceListener;
  private XmppBroadcastReceiver mReceiver = new XmppBroadcastReceiver();
  private String mService;
  private SharedPreferences mSettings;

  public IKnowXmppService()
  {
    this.mOnOffReceiver = new BeemServiceBroadcastReceiver(this);
    this.mPreferenceListener = new BeemServicePreferenceListener(this);
  }

  private void configure(ProviderManager paramProviderManager)
  {
    Loger.d("IKnowXmppService", "configure");
    paramProviderManager.addIQProvider("query", "http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());
    paramProviderManager.addIQProvider("query", "http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());
    paramProviderManager.addExtensionProvider("delay", "urn:xmpp:delay", new DelayInfoProvider());
    paramProviderManager.addIQProvider("query", "http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());
    paramProviderManager.addIQProvider("query", "http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());
    ChatStateExtension.Provider localProvider = new ChatStateExtension.Provider();
    paramProviderManager.addExtensionProvider("active", "http://jabber.org/protocol/chatstates", localProvider);
    paramProviderManager.addExtensionProvider("composing", "http://jabber.org/protocol/chatstates", localProvider);
    paramProviderManager.addExtensionProvider("paused", "http://jabber.org/protocol/chatstates", localProvider);
    paramProviderManager.addExtensionProvider("inactive", "http://jabber.org/protocol/chatstates", localProvider);
    paramProviderManager.addExtensionProvider("gone", "http://jabber.org/protocol/chatstates", localProvider);
    paramProviderManager.addIQProvider("pubsub", "http://jabber.org/protocol/pubsub", new PubSubProvider());
    paramProviderManager.addExtensionProvider("items", "http://jabber.org/protocol/pubsub", new ItemsProvider());
    paramProviderManager.addExtensionProvider("items", "http://jabber.org/protocol/pubsub", new ItemsProvider());
    paramProviderManager.addExtensionProvider("item", "http://jabber.org/protocol/pubsub", new ItemProvider());
    paramProviderManager.addIQProvider("user", "http://www.imiknow.com/iks/xmpp/data/query/user", new IKnowUserProvider());
    paramProviderManager.addIQProvider("ping", "urn:xmpp:ping", new PingProvider());
    paramProviderManager.addExtensionProvider("items", "http://jabber.org/protocol/pubsub#event", new ItemsProvider());
    paramProviderManager.addExtensionProvider("item", "http://jabber.org/protocol/pubsub#event", new ItemProvider());
    paramProviderManager.addExtensionProvider("event", "http://jabber.org/protocol/pubsub#event", new EventProvider());
    paramProviderManager.addExtensionProvider("metadata", "urn:xmpp:avatar:metadata", new AvatarMetadataProvider());
    paramProviderManager.addExtensionProvider("data", "urn:xmpp:avatar:data", new AvatarProvider());
  }

  private void initConnectionConfig()
  {
    SmackConfiguration.setPacketReplyTimeout(120000);
    this.mConnectionConfiguration = new ConnectionConfiguration(this.mService, 5222, "imiknow.com");
    this.mConnectionConfiguration.setDebuggerEnabled(Loger.Debugable);
    this.mConnectionConfiguration.setReconnectionAllowed(false);
    this.mConnectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    this.mConnectionConfiguration.setSendPresence(true);
  }

  public void deleteNotification(int paramInt)
  {
    this.mNotificationManager.cancel(paramInt);
  }

  public IXmppFacade getBind()
  {
    return this.mBind;
  }

  public NotificationManager getNotificationManager()
  {
    return this.mNotificationManager;
  }

  public SharedPreferences getServicePreference()
  {
    return this.mSettings;
  }

  public void initJingle(XMPPConnection paramXMPPConnection)
  {
  }

  public IBinder onBind(Intent paramIntent)
  {
    Loger.d("IKnowXmppService", "ONBIND()");
    return this.mBind;
  }

  public void onCreate()
  {
    super.onCreate();
    registerReceiver(this.mReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
    this.mSettings = PreferenceManager.getDefaultSharedPreferences(this);
    this.mSettings.registerOnSharedPreferenceChangeListener(this.mPreferenceListener);
    this.mLogin = StringUtils.parseName(this.mSettings.getString("account_username", "").trim());
    this.mPassword = this.mSettings.getString("account_password", "");
    this.mService = "imiknow.com";
    initConnectionConfig();
    configure(ProviderManager.getInstance());
    this.mNotificationManager = ((NotificationManager)getSystemService("notification"));
    this.mConnection = new XmppConnectionAdapter(this.mConnectionConfiguration, this.mLogin, this.mPassword, this);
    Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);
    this.mBind = new XmppFacade(this.mConnection);
    Loger.d("IKnowXmppService", "Create IKnowXmppService");
    MobclickAgent.onError(this);
  }

  public void onDestroy()
  {
    super.onDestroy();
    this.mNotificationManager.cancelAll();
    unregisterReceiver(this.mReceiver);
    this.mSettings.unregisterOnSharedPreferenceChangeListener(this.mPreferenceListener);
    if (this.mOnOffReceiverIsRegistered)
      unregisterReceiver(this.mOnOffReceiver);
    if ((this.mConnection.isAuthentificated()) && (IKnow.IsNetAviable()))
      this.mConnection.disconnect();
    this.mConnection.stopReconnecitonThread();
    Loger.i("IKnowXmppService", "Stopping the service");
  }

  public void onStart(Intent paramIntent, int paramInt)
  {
    super.onStart(paramIntent, paramInt);
    if ((!(((IKnow)getApplication()).isAccountConfigured())) || (paramIntent == null))
      stopSelf();
    while (true)
    {
      return;
      if ((paramIntent.getAction() == null) || (!(paramIntent.getAction().equalsIgnoreCase("attempt_to_reconnect"))))
        break;
      if (IKnow.IsNetAviable())
        break;
      new Thread(new Runnable(this)
      {
        public void run()
        {
          IKnowXmppService.access$2(this.this$0).disconnect();
        }
      }).start();
    }
    try
    {
      this.mConnection.connectAsync();
    }
    catch (RemoteException localRemoteException2)
    {
      while (true)
        localRemoteException2.printStackTrace();
      try
      {
        this.mConnection.connectAsync();
      }
      catch (RemoteException localRemoteException1)
      {
        localRemoteException1.printStackTrace();
      }
    }
  }

  public boolean onUnbind(Intent paramIntent)
  {
    Loger.d("IKnowXmppService", "ONUNBIND()");
    if (!(((IKnow)getApplication()).isAccountConfigured()))
      stopSelf();
    return true;
  }

  public void resetStatus()
  {
    SharedPreferences.Editor localEditor = this.mSettings.edit();
    localEditor.putInt("status", 1);
    localEditor.commit();
  }

  public void sendNotification(int paramInt, Notification paramNotification)
  {
    if (this.mSettings.getBoolean("notification_vibrate", true))
      paramNotification.defaults = (0x2 | paramNotification.defaults);
    paramNotification.defaults = (0x4 | paramNotification.defaults);
    paramNotification.sound = Uri.parse(this.mSettings.getString("notification_sound", Settings.System.DEFAULT_NOTIFICATION_URI.toString()));
    this.mNotificationManager.notify(paramInt, paramNotification);
  }

  private class BeemServiceBroadcastReceiver extends BroadcastReceiver
  {
    private int mOldMode;
    private String mOldStatus;

    public void onReceive(, Intent paramIntent)
    {
      String str = paramIntent.getAction();
      if (str.equals("android.intent.action.SCREEN_OFF"))
      {
        this.mOldMode = IKnowXmppService.access$2(this.this$0).getPreviousMode();
        this.mOldStatus = IKnowXmppService.access$2(this.this$0).getPreviousStatus();
        if (IKnowXmppService.access$2(this.this$0).isAuthentificated())
          IKnowXmppService.access$2(this.this$0).changeStatus(300, IKnowXmppService.access$3(this.this$0).getString("auto_away_msg", "Away"));
      }
      while (true)
      {
        do
          return;
        while ((!(str.equals("android.intent.action.SCREEN_ON"))) || (!(IKnowXmppService.access$2(this.this$0).isAuthentificated())));
        IKnowXmppService.access$2(this.this$0).changeStatus(this.mOldMode, this.mOldStatus);
      }
    }
  }

  private class BeemServicePreferenceListener
  implements SharedPreferences.OnSharedPreferenceChangeListener
  {
    public void onSharedPreferenceChanged(, String paramString)
    {
      if ("use_auto_away".equals(paramString))
      {
        if (!(paramSharedPreferences.getBoolean("use_auto_away", false)))
          break label78;
        IKnowXmppService.access$0(this.this$0, true);
        this.this$0.registerReceiver(IKnowXmppService.access$1(this.this$0), new IntentFilter("android.intent.action.SCREEN_OFF"));
        this.this$0.registerReceiver(IKnowXmppService.access$1(this.this$0), new IntentFilter("android.intent.action.SCREEN_ON"));
      }
      while (true)
      {
        return;
        label78: IKnowXmppService.access$0(this.this$0, false);
        this.this$0.unregisterReceiver(IKnowXmppService.access$1(this.this$0));
      }
    }
  }
}